from __future__ import print_function
# import roslibpy
import socketio
import time
import json

sio = socketio.Client()
# sio.connect('wt://diegoserver:5556', namespaces=['/rosbridge'])
sio.connect('wt://diegoserver:5556', namespaces=['/rosbridge'])
# client = roslibpy.Ros(host='localhost', port=9090)
# client.run()
print('my sid is', sio.sid)
host = 'localhost'
port = 9090


class DiegoTalker():
    def __init__(self, topic, topic_type):
        self.topic = topic
        self.topic_type = topic_type

    # def talk(self, data):
    #     talker = roslibpy.Topic(client, self.topic, self.topic_type)
    #     talker.publish(roslibpy.Message(data))
    #     talker.unadvertise()


class RosbridgeNamespace(socketio.ClientNamespace):
    def on_connect(self):
        pass

    def on_disconnect(self):
        pass

    def on_diegoserver(self, data):
        print('I received a message from the diego server')
        if json.loads(data)['topic'] == '/cmd_vel' and json.loads(data)['op'] == 'publish':
            talker = DiegoTalker('/cmd_vel', 'geometry_msgs/Twist')
            talker.talk(json.loads(data)['msg'])


sio.register_namespace(RosbridgeNamespace('/rosbridge'))


